package cn.com.miaoto.business.controller;

import cn.com.miaoto.base.common.BaseController;
import cn.com.miaoto.base.common.PageResult;
import cn.com.miaoto.base.common.PageSearch;
import cn.com.miaoto.base.common.ReturnMsg;
import cn.com.miaoto.business.model.Project;
import cn.com.miaoto.business.service.ProjectService;
import cn.com.miaoto.business.vo.ProjectInfo;
import cn.com.miaoto.model.LoginUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * Created by guo.chen on 2018/9/17.
 */
@RestController
@RequestMapping("project")
public class ProjectController extends BaseController {

    @Resource
    private ProjectService projectService;

    @GetMapping("selectPageList")
    public PageResult selectLoginUserAll(HttpServletRequest request, PageSearch pageSearch){
        Map params = getParameterMap(request);
        try{
            PageHelper.startPage(pageSearch.getPage(), pageSearch.getLimit());
            List<ProjectInfo> projectList = this.projectService.selectProjectList(params);
            return new PageResult<ProjectInfo>(projectList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("查询项目列表失败");
            return new PageResult();
        }
    }

    @GetMapping("selectByPrimaryKey")
    public ReturnMsg selectByPrimaryKey(String id){
        try{
            Project project = this.projectService.selectByKey(id);
            return new ReturnMsg(true,project);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("根据主键查询项目信息失败");
            return new ReturnMsg(false);
        }
    }

    /**
     * 按月份统计新增项目数量
     * @return
     */
    @GetMapping("countByMonth")
    public List<Map> countByMonth(HttpServletRequest request){
        Map params = getParameterMap(request);
        try{
            return this.projectService.countByMonth(params);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("按月份统计新增项目数量失败");
            return null;
        }
    }

    /**
     * 按年份统计新增项目数量
     * @return
     */
    @GetMapping("countByYear")
    public List<Map> countByYear(HttpServletRequest request){
        Map params = getParameterMap(request);
        try{
            return this.projectService.countByYear(params);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("按年份统计新增项目数量失败");
            return null;
        }
    }

    @PostMapping("edit")
    public ReturnMsg insertOrUpdate(HttpSession session, @RequestBody  Project project){
        LoginUser loginUser =(LoginUser) session.getAttribute("currentUser");
        try {
            if(StringUtils.isEmpty(project.getId())){
                project.setProgramstatus("项目立项");
                project.setCreator(loginUser.getUserId());
                project.setCreateTime(LocalDateTime.now());
                return new ReturnMsg(this.projectService.saveNotNull(project)>0,"新增项目信息成功");
            } else{
                project.setCreator(loginUser.getUserId());
                return new ReturnMsg(this.projectService.updateNotNull(project)>0,"修改项目信息成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("新增项目信息失败");
            return new ReturnMsg(false);
        }
    }

    @DeleteMapping("delete")
    public ReturnMsg delete(String id){
        try {
            return new ReturnMsg(this.projectService.delete(id)>0);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除项目信息失败");
            return new ReturnMsg(false);
        }
    }

    @DeleteMapping("deleteByIds")
    public ReturnMsg deleteByIds(String ids){
        try {
            return new ReturnMsg(this.projectService.deleteByIds(ids)>0);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除项目信息失败");
            return new ReturnMsg(false);
        }
    }
}
